package test;

import org.jing.core.lang.annotation.SerializableField;
import org.jing.core.lang.annotation.SerializableType;
import org.jing.core.lang.data.BaseSerializableDto;
import org.jing.core.lang.data.Carrier;
import org.jing.core.lang.data.DateSerializableFactory;
import org.jing.core.logger.JingLogger;
import org.jing.core.util.GenericUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Description: <br>
 *
 * @author bks <br>
 * @since 2020-06-29 <br>
 */
public class Demo4Carrier {
    private static final JingLogger LOGGER = JingLogger.getLogger(Demo4Carrier.class);

    private Demo4Carrier() {
        List<String> strList = GenericUtil.<String>buildList().add("1").add("2").add("3").get();
        List<Map<String, Object>> mapList = GenericUtil.<Map<String, Object>>buildList()
                .add(GenericUtil.<String, Object>buildMap().put("index", 1).put("value", "map-1").get())
                .add(GenericUtil.<String, Object>buildMap().put("index", 2).put("value", "map-2").get())
                .add(GenericUtil.<String, Object>buildMap().put("index", 3).put("value", "map-3").get())
                .get();
        List<Carrier> carrierList = GenericUtil.<Carrier>buildList()
                .add(new Carrier().setNumber("index", 1).setString("value", "carrier-1"))
                .add(new Carrier().setNumber("index", 2).setString("value", "carrier-2"))
                .add(new Carrier().setNumber("index", 3).setString("value", "carrier-3"))
                .get();
        List<Temp> dtoList = GenericUtil.<Temp>buildList()
                .add(new Temp("temp-1", new Date(), BigDecimal.valueOf(1)))
                .add(new Temp("temp-2", new Date(), BigDecimal.valueOf(2)))
                .add(new Temp("temp-3", new Date(), BigDecimal.valueOf(3)))
                .get();
        List<Temp> tempList = GenericUtil.<Temp>buildList().get();
        Carrier carrier = new Carrier();
        carrier.addString("name", "lxd1");
        carrier.setString("date", "20250109");
        carrier.addString("name", "lxd2");
        carrier.addList("str", strList);
        carrier.addList("map", mapList);
        carrier.addList("carrier", carrierList, t -> t.setString("temp", "temp"));
        carrier.addList("dto", dtoList, d -> {
            Carrier c = Carrier.parseSerializableDto(d);
            c.setString("temp", "temp");
            return c;
        });
        carrier.addList("temp", tempList, d -> {
            Carrier c = Carrier.parseSerializableDto(d);
            c.setString("temp", "temp");
            return c;
        });
        LOGGER.info(carrier.asJson());
    }

    @SerializableType
    private static class Temp extends BaseSerializableDto {
        @SerializableField
        private String name;

        @SerializableField(factory = DateSerializableFactory.class, factoryArgs = "yyyyMMdd")
        private Date date;

        @SerializableField(defaultValue = "0.1", defaultValueType = BigDecimal.class)
        private BigDecimal number;

        private Temp(String name, Date date, BigDecimal number) {
            this.name = name;
            this.date = date;
            this.number = number;
        }
    }

    public static void main(String[] args) {
        new Demo4Carrier();
    }
}
